package com.animee.app4.utils;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;

public class DBManager {
    static DatabaseHelper helper;   //通过这个类，可以获取到数据库对象db
    static int version = 1;   //数据库版本号
    static RdbStore rdbStore ;    //数据库类的引用
    public static RdbStore getRdbStore(Context context){
        if (rdbStore == null){
            initDB(context);
        }
        return rdbStore;
    }

    private static void initDB(Context context){
        helper = new DatabaseHelper(context);
        StoreConfig config = StoreConfig.newDefaultConfig("test.db");

        rdbStore = helper.getRdbStore(config, version, new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                String sql = "create table if not exists newtb(" +
                        "uniquekey varchar(100) PRIMARY KEY,title text,date varchar(100),category varchar(30)," +
                        "author_name varchar(100),url TEXT,thumbnail_pic_s TEXT,thumbnail_pic_s02 TEXT," +
                        "thumbnail_pic_s03 TEXT,is_content varchar(20))";
                rdbStore.executeSql(sql);
            }
            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {

            }
        });
    }
}